$(document).ready(function() {
    var ctx = $("#ctx").val(); //获取服务器地址
    //开启,暂停,删除
    $("#btn-enable,#btn-disable,#btn-delete").click(function() {
        if ($(this).attr("id") == "btn-enable") {
            url = ctx + "/back/main/role/enable";
        } else if ($(this).attr("id") == "btn-disable") {
            url = ctx + "/back/main/role/unEnable";
        } else if ($(this).attr("id") == "btn-delete") {
            url = ctx + "/back/main/resources/delete";
        }
        singleOper($(this), url);

    });
    //批量删除
    $("#all-btn-delete").click(function() {
        url = ctx + "/back/main/resources/deleteall";
        batchOpear($(this), url);

    });

    //父类删除子类也删除
    $(".father_de").click(function() {
        a = $(this).attr("value");
        b = "." + a + "a";
        bootbox.confirm("确定删除吗?", function(ensure) {
            var ids = [];
            var id = parseInt($(".father_de").attr("value"));
            ids.push(id);

            url = ctx + "/back/main/resources/deleteall";
            $(b).each(function() {
                var id = parseInt($(this).attr("id"));
                ids.push(id);
            });
            $.ajax({
                async: false,
                traditional: true,
                type: "POST",
                url: url,
                data: {
                    "id": ids
                },
                success: function(data) {
                    refreshData();
                }
            });
        });
    });

    //取消
    $("#footBtn .btn-cancel").click(function() {
        window.location.href = ctx + "/back/main/resources/list";
    });
    //初始隐藏子列表
    init_ele = $('.toggle_button');
    for (i = 0; i < init_ele.length; i++) {
        a = init_ele.get(i);
        init_ele_id = "." + a.id;
        $(init_ele_id).hide();
    }
    //点击按钮显示和隐藏子列表
    init_id = "." + $('.toggle_button').attr("id");
    $(init_id).hide();
    $('.toggle_button').click(function() {
        id = "." + $(this).attr("id");
        $(id).toggle();
    });
    //菜单类型如果是目录，则上级菜单改为顶级目录
    $("select[name=type] option[value='0'] ").click(function() {
        $("select[name=parentId]").find("option:selected").prop("selected", false);
    })
});

//菜单类型如果是目录，则上级菜单改为顶级目录
function toVaild() {
    if ($("select[name=type]").find("option:selected").val() == 0) {
        $("select[name=parentId]").find("option:selected").prop("selected", false);
        return true;
    } else if ($("select[name=type]").find("option:selected").val() == 1 | 2) {
        return true;
    } else {
        alert("请选择正确的目录!");
        return false;
    }

}